package com.baosight.innerinterface.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.baosight.innerinterface.domain.SOSO02;
import com.baosight.innerinterface.domain.Tsosl05;
import com.baosight.innerinterface.service.SOUtils;
import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.iplat4j.ed.md.api.SeqGeneraterFacade;
import com.baosight.iplat4j.ep.ServiceEPBase;

public class ServiceDesignIn extends ServiceEPBase{
	Dao dao = (Dao) SpringApplicationContext.getBean("dao");
	
	/**
	 * 接收质检信息并新增数据库
	 * 
	 * @param inInfo
	 * @return
	 */
	public EiInfo syncQualityInfo(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		try {
			for (int num = 0; num < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); num++) {
				Map<Object, Object> map = inInfo.getBlock(EiConstant.resultBlock).getRow(num);
				String batchNo = map.get("fdMyNum").toString();

				EiBlock block = new EiBlock("bean");
				for (Map.Entry<Object, Object> entry : map.entrySet()) {
					if(!(NumberUtils.isNumber(entry.getValue().toString()))){
						continue;
					}     
					String key = entry.getKey().toString();//传入的字段名
					BigDecimal value = new BigDecimal(entry.getValue().toString());//传入的字段值
					
					// 根据综合生产计划的表结构字段查询我们系统定义的代码
					String beanCode = SOUtils.getItemValueForIplat("TEDCM01", "ITEM_CODE", "ITEM_ENAME", key);
					if(StringUtils.isBlank(beanCode)){
						continue;
					}
					
					SOSO02 soso02 = new SOSO02();
					soso02.setBeanCode(beanCode);
					soso02.setBeanName(SOUtils.getItemValue("bssim.sp.elementType", beanCode));
					soso02.setBeanValue(value);
					block.addRow(soso02);
				}

				String xmlStr = SOUtils.createQualityParaXml(block);
				String descStr = SOUtils.createQualityDesc(block);

				Tsosl05 tsosl05 = new Tsosl05();
				tsosl05.setBatchNo(batchNo);
				tsosl05.setDataSourceType("10");
				tsosl05.setQualityDesc(descStr);
				tsosl05.setQualityPara(xmlStr);
				
				String idGenerator = ((SeqGeneraterFacade) SpringApplicationContext.getBean("edSeqGeneraterFacade"))
						.getNextValue("Tsosl05", 8);
				
				String stackingNo = tsosl05.getStackingNo();
				if (StringUtils.isBlank(stackingNo)) {
//					if (SOUtils.getCompanyCode() == null && UserSession.getInSessionProperty("account")==null) {
//						throw new Exception("无法获取当前登录人的公司别信息!");
//					}
//					stackingNo = SOUtils.getSeqValue(SOUtils.getCompanyCode(), "STACKING_NO");
					stackingNo = "STACKING_NO_SZ0000";
					tsosl05.setStackingNo(stackingNo);
				}

				Map<Object, Object> param = new HashMap<Object, Object>();
				param.put("batchNo", batchNo);
				Integer seqNo = SOUtils.getSeq(1, "InDesignInterface.getQualitySeq", param);

				tsosl05.setId(Integer.valueOf(idGenerator));
				tsosl05.setSeqNo(seqNo);
				tsosl05.setBatchNo(batchNo);
				
				dao.insert("InDesignInterface.insert", tsosl05);
			}
			outInfo.setMsg("同步质检信息成功");
			return outInfo;
		} catch (Exception e) {
			outInfo.setMsg(e.getMessage());
			outInfo.setStatus(EiConstant.STATUS_FAILURE);
			return outInfo;
		}
	}
}
